19  CJS [t,t] - growth model

Simple CJS model integrated with a growth in weight model to get phi, p, and growth estimates to compare with Neural Network CMR/growth model

In all the models below, 1 = not observed and 2 = observed in the input encounter data.
Encounter data are available here in the eh.csv file. Weight data are in weight.csv

19.0.1 modelNum 1: g(i,t) model

Growth rate (gr) model structure:

gr(t,i) <- grInt(t,i)

Model code is in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R
Model is run ‘by hand’ in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_makeFile.R
Not using this: Targets are loaded in R/modelCMR_tt_growth_NN_OB.R and saved as modelCMR_tt_growth_NN_OB_target

Model runs:

19.0.1.1 Retrieve model results

Code
library(targets)
# Following https://oliviergimenez.github.io/bayesian-cr-workshop/worksheets/4_demo.html
# 
modelNum = 1 # growth only
dummy=2

#load('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_mostRecent.RData')
load(paste0('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_model', modelNum, '_mostRecent.RData'))
out_NN_OBmod1 <- d
rm(d)

#Input data
eh <- tar_read(eh_OB_2002_2014_target)

19.0.1.2 Model code

In the model code, a value of 1 for z or in gamma or omega signifies the individual is alive and a value of 2 signifies the individual is dead. y[i,j] is the observed data (encounter history file).

Code
out_NN_OBmod1$modelCode
{
    for (i in 1:N) {
        weight[i, first[i]] ~ dnorm(meanWeight_AIS[first[i]], 
            sd = 2)
        weightDATA[i, first[i]] ~ dnorm(weight[i, first[i]], 
            sd = weightSD)
        for (t in (first[i]):(last[i] - 1)) {
            weight[i, t + 1] <- weight[i, t] + gr[i, t] * sampleIntervalDATA[i, 
                t]/90
            weightDATA[i, t + 1] ~ dnorm(weight[i, t + 1], sd = weightSD)
        }
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            gr[i, t] ~ dnorm(grIntT[t], sd = 0.5)
        }
    }
    for (t in 1:(T - 1)) {
        grIntT[t] ~ dnorm(0, sd = 1000)
    }
}

19.0.1.3 Model statistics

Run statistics
nIter nBurnin nChains thinRate
5000 2500 2 5

[1] “Run time = 37.263 mins”

19.0.1.4 Plot traces

Code
  priors <- rnorm(out_NN_OBmod1$runData$nIter * out_NN_OBmod1$runData$nChains, 0, 1000)
  MCMCtrace(object = out_NN_OBmod1$mcmc,
            #ISB = FALSE,
            #exact = TRUE, 
            params = c("grIntT"),
            pdf = FALSE, 
            priors = priors)

19.0.1.5 Summary data

Code
  MCMCplot(object = out_NN_OBmod1$mcmc, params = c("grIntT"))

Summary values

Code
#|
  (summaryMod1_tt_growth <- MCMCsummary(object = out_NN_OBmod1$mcmc, params = c("grIntT"), round = 3) %>%
    rownames_to_column(var = "var"))
          var   mean    sd   2.5%    50%  97.5% Rhat n.eff
1   grIntT[1]  1.171 0.026  1.119  1.172  1.218 1.00   158
2   grIntT[2]  1.124 0.023  1.079  1.124  1.168 1.01    95
3   grIntT[3]  9.274 0.029  9.220  9.273  9.336 1.00    93
4   grIntT[4]  3.325 0.023  3.278  3.325  3.369 1.01   150
5   grIntT[5]  0.592 0.031  0.533  0.590  0.657 1.07   132
6   grIntT[6]  1.549 0.032  1.485  1.551  1.610 1.00   129
7   grIntT[7] 17.131 0.045 17.050 17.130 17.216 1.02    71
8   grIntT[8]  7.416 0.056  7.313  7.417  7.531 1.04    96
9   grIntT[9] -1.551 0.094 -1.717 -1.558 -1.356 1.00    35
10 grIntT[10]  4.074 0.095  3.875  4.078  4.254 1.03    32
11 grIntT[11] 22.904 0.115 22.682 22.903 23.116 1.34    34
Code
  # To get the mMCMCSummaryRMNA funcion which I adapted to deal with NAs
  source('./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R')
  
  #summaryMod1_tt_growth_z0 <- MCMCSummaryRMNA(object = out_NN_OBmod1$mcmc, params = c("weight"), round = 3) %>%
  summaryMod1_tt_growth_zGR <- MCMCSummaryRMNA(object = out_NN_OBmod1$mcmc, params = c("gr"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod1_tt_growth_zW <- MCMCSummaryRMNA(object = out_NN_OBmod1$mcmc, params = c("weight"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod1_tt_growth_z0 <- summaryMod1_tt_growth_zW |> 
    left_join(summaryMod1_tt_growth_zGR, by = c("ind", "t"), suffix = c("_weight", "_gr"))

Add other variables to summary values

Code
  ehLong <- 
    eh$eh |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      enc = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))

  firstLong <- eh$first |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(first = value)
  
  lastLong <- eh$last |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(last = value)
    
  cohortLong <- eh$cohort |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) 
  
  weightLong <- 
    eh$weight |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      weightDATA = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))
  
  summaryMod1_tt_growth_z <- summaryMod1_tt_growth_z0 |> 
    left_join(ehLong) |> 
    mutate(
      # meanM1 = mean - 1,
      # pSurv = ifelse(meanM1 == 0, 1, 1 - meanM1),
      enc8 = ifelse(enc == 1, 0.8, 0)
    ) |> 
    left_join(firstLong) |> 
    left_join(lastLong) |> 
    left_join(cohortLong) |> 
    left_join(weightLong) |> 
    mutate(
      resid = weightDATA - mean_weight
    ) 
Code
  resids <- summaryMod1_tt_growth_z |> 
    group_by(ind) |> 
    summarize(
      meanResid = mean(abs(resid), na.rm = TRUE),
      n = n()
    ) |> 
    ungroup()

  summaryMod1_tt_growth_zR <- 
    summaryMod1_tt_growth_z |> 
    left_join(resids)
Code
#  write.csv(summaryMod1_tt_growth_zR, './data/outForObservable/summaryMod1_tt_growth_zR.csv')
Code
ojs_define(numTags_OJS = dim(eh$tags)[1])
ojs_define(summaryMod1_tt_growth_zR_OJS = (summaryMod1_tt_growth_zR))

19.0.1.6 Plot predicted and observed masses for selected individuals

Select one or more individuals:

Code
viewof selectInd = Inputs.select([...Array(numTags_OJS).keys()], {
  label: "Which fish?",
  value: 1,
  step: 1,
  multiple: true
})

summaryMod1_tt_growth_zR_OJS_T = transpose(summaryMod1_tt_growth_zR_OJS)
summaryMod1_tt_growth_zR_OJS_T_selected = summaryMod1_tt_growth_zR_OJS_T.filter((d) =>
  selectInd.includes(d.ind)
)

Black dots/line is estimated mass and orange dots are observed masses. The green line is the first observation and the red line is the last observation. The number in the upper right corner of each panel is the fish’s cohort. Green dots are estimated growth rates.

Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Age/season combination" },
  y: { label: "Body mass (mg)" },
  marks: [
    Plot.frame(),
    Plot.dot(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    Plot.line(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    Plot.dot(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "t",
      y: "mean_gr",
      fill: "green"
    }),
    Plot.dot(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "t",
      y: "weightDATA",
      fill: "orange"
    }),
    Plot.ruleX(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "first",
      y: 3,
      stroke: "green"
    }),
    Plot.ruleX(summaryMod1_tt_growth_zR_OJS_T_selected, {
      x: "last",
      y: 3,
      stroke: "red"
    }),
    Plot.text(summaryMod1_tt_growth_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 60,
        frameAnchor: "top-left",
        text: (d) => "Cohort = " + d.cohort
      })
    ),
    Plot.text(summaryMod1_tt_growth_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 56,
        frameAnchor: "top-left",
        text: (d) => "Residual = " + d.meanResid
      })
    )
  ],
  facet: {
    data: summaryMod1_tt_growth_zR_OJS_T_selected,
    x: "ind"
  }
})

19.0.2 modelNum 2: phi(i,t) + g(i,t), p(i,t) model

Model with phi and p for each age-in-samples (t = column in the encounter history file) and individual (i)

Probability of survival (phi) model structure:

phi(t,i) <- phiInt(t,i)

Probability of capture (p) model structure:

p(t,i) <- pInt(t,i)

Growth rate (gr) model structure:

gr(t,i) <- grInt(t,i)

Survival/growth rate interaction:

Additive

Model code is in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R
Model is run ‘by hand’ in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_makeFile.R
Not using this: Targets are loaded in R/modelCMR_tt_growth_NN_OB.R and saved as modelCMR_tt_growth_NN_OB_target

Model runs:

19.0.2.1 Retrieve model results

Code
library(targets)
# Following https://oliviergimenez.github.io/bayesian-cr-workshop/worksheets/4_demo.html
# 
modelNum = 2 # phi + growth

#load('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_mostRecent.RData')
load(paste0('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_model', modelNum, '_mostRecent.RData'))
out_NN_OBmod2 <- d
rm(d)

#Input data
eh <- tar_read(eh_OB_2002_2014_target)

19.0.2.2 Model code

In the model code, a value of 1 for z or in gamma or omega signifies the individual is alive and a value of 2 signifies the individual is dead. y[i,j] is the observed data (encounter history file).

Code
out_NN_OBmod2$modelCode
{
    for (i in 1:N) {
        weight[i, first[i]] ~ dnorm(meanWeight_AIS[first[i]], 
            sd = 2)
        weightDATA[i, first[i]] ~ dnorm(weight[i, first[i]], 
            sd = weightSD)
        for (t in (first[i]):(last[i] - 1)) {
            weight[i, t + 1] <- weight[i, t] + gr[i, t] * sampleIntervalDATA[i, 
                t]/90
            weightDATA[i, t + 1] ~ dnorm(weight[i, t + 1], sd = weightSD)
        }
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            gr[i, t] ~ dnorm(grIntT[t], sd = 0.5)
        }
    }
    for (t in 1:(T - 1)) {
        grIntT[t] ~ dnorm(0, sd = 1000)
    }
    delta[1] <- 1
    delta[2] <- 0
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            gamma[1, 1, t, i] <- phi[i, t]
            gamma[1, 2, t, i] <- 1 - phi[i, t]
            gamma[2, 1, t, i] <- 0
            gamma[2, 2, t, i] <- 1
        }
    }
    for (t in 1:(T - 1)) {
        p[t] ~ dunif(0, 1)
        omega[1, 1, t] <- 1 - p[t]
        omega[1, 2, t] <- p[t]
        omega[2, 1, t] <- 1
        omega[2, 2, t] <- 0
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            logit(phi[i, t]) <- phiInt[i, t]
        }
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            phiInt[i, t] ~ dnorm(phiIntT[t], sd = 2)
        }
    }
    for (t in 1:(T - 1)) {
        phiIntT[t] ~ T(dnorm(0, sd = 1.5), -3.5, 3.5)
        phiIntTOut[t] <- ilogit(phiIntT[t])
    }
    for (i in 1:N) {
        z[i, first[i]] ~ dcat(delta[1:2])
        for (j in first[i]:(last[i] - 1)) {
            z[i, j + 1] ~ dcat(gamma[z[i, j], 1:2, j, i])
            yDATA[i, j + 1] ~ dcat(omega[z[i, j + 1], 1:2, j])
        }
    }
}

19.0.2.3 Model statistics

Run statistics
nIter nBurnin nChains thinRate
7500 5000 2 5

[1] “Run time = 2.231 hours”

19.0.2.4 Plot traces

Code
  priors <- rnorm(out_NN_OBmod2$runData$nIter * out_NN_OBmod2$runData$nChains, 0, 1000)
  MCMCtrace(object = out_NN_OBmod2$mcmc,
            #ISB = FALSE,
            #exact = TRUE, 
            params = c("phiIntTOut", "grIntT"),
            pdf = FALSE, 
            priors = priors)

19.0.2.5 Summary data

Code
  MCMCplot(object = out_NN_OBmod2$mcmc, params = c("phiIntTOut"))

Code
  MCMCplot(object = out_NN_OBmod2$mcmc, params = c("p"))

Code
  MCMCplot(object = out_NN_OBmod2$mcmc, params = c("grIntT"))

Summary values

Code
#|
(summaryMod2_tt_growth <- MCMCsummary(object = out_NN_OBmod2$mcmc, params = c("phiIntTOut", "p", "grIntT"), round = 3) %>%
    rownames_to_column(var = "var"))
              var   mean    sd   2.5%    50%  97.5% Rhat n.eff
1   phiIntTOut[1]  0.913 0.017  0.878  0.914  0.942 1.03    42
2   phiIntTOut[2]  0.903 0.017  0.867  0.905  0.930 1.12    55
3   phiIntTOut[3]  0.861 0.023  0.813  0.862  0.902 1.69    33
4   phiIntTOut[4]  0.729 0.034  0.655  0.731  0.799 2.26    39
5   phiIntTOut[5]  0.714 0.037  0.646  0.713  0.791 1.86    37
6   phiIntTOut[6]  0.791 0.036  0.703  0.797  0.845 1.10    32
7   phiIntTOut[7]  0.884 0.028  0.801  0.886  0.933 1.07    18
8   phiIntTOut[8]  0.258 0.040  0.177  0.260  0.330 1.03    25
9   phiIntTOut[9]  0.708 0.074  0.556  0.709  0.838 2.48    14
10 phiIntTOut[10]  0.675 0.086  0.484  0.686  0.850 2.59    13
11 phiIntTOut[11]  0.649 0.116  0.485  0.617  0.888 1.03    20
12           p[1]  0.595 0.021  0.557  0.595  0.637 1.01   282
13           p[2]  0.451 0.018  0.417  0.451  0.486 1.04   346
14           p[3]  0.706 0.021  0.668  0.706  0.747 1.30   126
15           p[4]  0.654 0.022  0.609  0.654  0.696 1.27   127
16           p[5]  0.633 0.025  0.584  0.633  0.680 1.05   220
17           p[6]  0.494 0.027  0.442  0.494  0.545 1.00   452
18           p[7]  0.730 0.035  0.663  0.729  0.798 1.01   113
19           p[8]  0.781 0.050  0.675  0.782  0.878 1.04   289
20           p[9]  0.634 0.062  0.513  0.637  0.749 1.31   439
21          p[10]  0.673 0.081  0.511  0.677  0.822 1.03   245
22          p[11]  0.802 0.125  0.552  0.815  0.987 1.07   102
23      grIntT[1]  1.172 0.028  1.117  1.171  1.229 1.06   119
24      grIntT[2]  1.120 0.023  1.076  1.119  1.168 1.01   106
25      grIntT[3]  9.281 0.029  9.222  9.281  9.337 1.20    92
26      grIntT[4]  3.324 0.022  3.281  3.324  3.366 1.00   281
27      grIntT[5]  0.583 0.030  0.525  0.583  0.638 1.00   117
28      grIntT[6]  1.558 0.032  1.495  1.557  1.620 1.09   139
29      grIntT[7] 17.114 0.041 17.035 17.113 17.200 1.10   102
30      grIntT[8]  7.416 0.055  7.306  7.417  7.528 1.02    81
31      grIntT[9] -1.559 0.083 -1.719 -1.559 -1.402 1.00    45
32     grIntT[10]  4.095 0.091  3.917  4.098  4.284 1.10    43
33     grIntT[11] 22.873 0.116 22.647 22.863 23.117 1.45    30
Code
  # To get the mMCMCSummaryRMNA funcion which I adapted to deal with NAs
  source('./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R')
  
  #summaryMod2_tt_growth_z0 <- MCMCSummaryRMNA(object = out_NN_OBmod2$mcmc, params = c("weight"), round = 3) %>%
  summaryMod2_tt_w <- MCMCSummaryRMNA(object = out_NN_OBmod2$mcmc, params = c("weight"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod2_tt_z <- MCMCSummaryRMNA(object = out_NN_OBmod2$mcmc, params = c("z"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod2_tt_z0 <- summaryMod2_tt_z |> 
    left_join(summaryMod2_tt_w, by = c("ind", "t"), suffix = c("_z", "_weight"))

Add other variables to summary values

Code
  ehLong <- 
    eh$eh |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      enc = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))
  
  firstLong <- eh$first |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(first = value)
  
  lastLong <- eh$last |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(last = value)
    
  cohortLong <- eh$cohort |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) 
  
  weightLong <- 
    eh$weight |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      weightDATA = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))
  
  summaryMod2_tt_z <- summaryMod2_tt_z0 |> 
    left_join(ehLong) |> 
    mutate(
      meanM1 = mean_z - 1,
      pSurv = ifelse(meanM1 == 0, 1, 1 - meanM1),
      enc8 = ifelse(enc == 1, 0.8, 0)
    ) |> 
    left_join(firstLong) |> 
    left_join(lastLong) |> 
    left_join(cohortLong) |> 
    left_join(weightLong) |> 
    mutate(
      resid = weightDATA - mean_weight
    ) 
    
  # summaryMod2_tt_z <- summaryMod2_tt_z0 |> 
  #   left_join(ehLong) |> 
  #   mutate(
  #     # meanM1 = mean - 1,
  #     # pSurv = ifelse(meanM1 == 0, 1, 1 - meanM1),
  #     enc8 = ifelse(enc == 1, 0.8, 0)
  #   ) |> 
  #   left_join(firstLong) |> 
  #   left_join(lastLong) |> 
  #   left_join(cohortLong) 
Code
  resids_mod2 <- summaryMod2_tt_z |> 
    group_by(ind) |> 
    summarize(
      meanResid = mean(abs(resid), na.rm = TRUE),
      n = n()
    ) |> 
    ungroup()

  summaryMod2_tt_zR <- 
    summaryMod2_tt_z |> 
    left_join(resids_mod2)
Code
ojs_define(numTags_OJS_mod2 = dim(eh$tags)[1])
ojs_define(summaryMod2_tt_zR_OJS = (summaryMod2_tt_zR))
#ojs_define(summary_tt_z_OJS = (summary_tt_z_mod2))

19.0.2.6 Plot predicted and observed masses for selected individuals

Select one or more individuals:

Code
viewof selectInd_mod2 = Inputs.select([...Array(numTags_OJS).keys()], {
  label: "Which fish?",
  value: 1,
  step: 1,
  multiple: true
})

summaryMod2_tt_zR_OJS_T = transpose(summaryMod2_tt_zR_OJS)
summaryMod2_tt_zR_OJS_T_selected = summaryMod2_tt_zR_OJS_T.filter((d) =>
  selectInd_mod2.includes(d.ind)
)

Black dots/line is estimated mass and orange dots are observed masses. The green line is the first observation and the red line is the last observation. The number in the upper right corner of each panel is the fish’s cohort.

Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Age/season combination" },
  y: { label: "Body mass (mg)" },
  marks: [
    Plot.frame(),
    Plot.dot(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    Plot.line(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    <!-- Plot.dot(summaryMod2_tt_zR_OJS_T_selected, { -->
    <!--   x: "t", -->
    <!--   y: "mean_gr", -->
    <!--   fill: "green" -->
    <!-- }), -->
    Plot.dot(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "weightDATA",
      fill: "orange"
    }),
    Plot.ruleX(summaryMod2_tt_zR_OJS_T_selected, {
      x: "first",
      y: 3,
      "stroke": "green"
    }),
    Plot.ruleX(summaryMod2_tt_zR_OJS_T_selected, {
      x: "last",
      y: 3,
      "stroke": "red"
    }),
    Plot.text(summaryMod2_tt_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 60,
        frameAnchor: "top-left",
        text: (d) => "Cohort = " + d.cohort
      })
    ),
    Plot.text(summaryMod2_tt_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 56,
        frameAnchor: "top-left",
        text: (d) => "Residual = " + d.meanResid
      })
    )
  ],
  facet: {
    data: summaryMod2_tt_zR_OJS_T_selected,
    x: "ind"
  }
})

19.0.2.7 Plot survival

Black dots/line is estimated probability of survival and orange dots are encountered yes (y = 0.8)/no (y = 0). The green line is the first observation and the red line is the last observation. The number in the upper right corner of each panel is the fish’s cohort.

Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Age/season combination" },
  y: { label: "Probability of survival" },
  marks: [
    Plot.frame(),
    Plot.dot(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "pSurv"
    }),
    Plot.dot(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "enc8",
      fill: "orange"
    }),
    Plot.line(summaryMod2_tt_zR_OJS_T_selected, {
      x: "t",
      y: "pSurv"
    }),
    Plot.ruleX(summaryMod2_tt_zR_OJS_T_selected, {
      x: "first",
      y: 1,
      "stroke": "green"
    }),
    Plot.ruleX(summaryMod2_tt_zR_OJS_T_selected, {
      x: "last",
      y: 1,
      "stroke": "red"
    })
    ,
    Plot.text(summaryMod2_tt_zR_OJS_T_selected, 
      Plot.selectFirst({
        x: 11,
        y: 1,
        text: d => d.cohort
      })
    )
  ],
  facet: {
    data: summaryMod2_tt_zR_OJS_T_selected,
    x: "ind"
  }
})

19.0.3 modelNum 3: phi(i,t) * g(i,t), p(i,t) model

Model with phi and p for each age-in-samples (t = column in the encounter history file) and individual (i)

Probability of survival (phi) model structure:

phi(t,i) <- phiInt(t,i) + weight(t,i) + weight(t,i)^2

Probability of capture (p) model structure:

p(t,i) <- pInt(t,i)

Growth rate (gr) model structure:

gr(t,i) <- grInt(t,i)

Survival/growth rate interaction:

Multiplicative

Model code is in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R
Model is run ‘by hand’ in ./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_makeFile.R
Not using this: Targets are loaded in R/modelCMR_tt_growth_NN_OB.R and saved as modelCMR_tt_growth_NN_OB_target

Model runs:

19.0.3.1 Retrieve model results

Code
library(targets)
# Following https://oliviergimenez.github.io/bayesian-cr-workshop/worksheets/4_demo.html
# 
modelNum = 3 # phi + growth

#load('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_mostRecent.RData')
load(paste0('./models/cmrNN_OB/tt_growth/runsOut/tt_growth_NN_OB_model', modelNum, '_mostRecent.RData'))
out_NN_OBmod3 <- d
rm(d)

#Input data
eh <- tar_read(eh_OB_2002_2014_target)

19.0.3.2 Model code

In the model code, a value of 1 for z or in gamma or omega signifies the individual is alive and a value of 2 signifies the individual is dead. y[i,j] is the observed data (encounter history file).

Code
out_NN_OBmod3$modelCode
{
    for (i in 1:N) {
        weight[i, first[i]] ~ dnorm(meanWeight_AISstd[first[i]], 
            sd = 2)
        weightDATAstd[i, first[i]] ~ dnorm(weight[i, first[i]], 
            sd = weightSD)
        for (t in (first[i]):(last[i] - 1)) {
            weight[i, t + 1] <- weight[i, t] + gr[i, t] * sampleIntervalDATA[i, 
                t]/90
            weightDATAstd[i, t + 1] ~ dnorm(weight[i, t + 1], 
                sd = weightSD)
        }
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            gr[i, t] ~ dnorm(grIntT[t], sd = 0.5)
        }
    }
    for (t in 1:(T - 1)) {
        grIntT[t] ~ dnorm(0, sd = 1000)
    }
    delta[1] <- 1
    delta[2] <- 0
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            gamma[1, 1, t, i] <- phi[i, t]
            gamma[1, 2, t, i] <- 1 - phi[i, t]
            gamma[2, 1, t, i] <- 0
            gamma[2, 2, t, i] <- 1
        }
    }
    for (t in 1:(T - 1)) {
        p[t] ~ dunif(0, 1)
        omega[1, 1, t] <- 1 - p[t]
        omega[1, 2, t] <- p[t]
        omega[2, 1, t] <- 1
        omega[2, 2, t] <- 0
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            logit(phi[i, t]) <- phiInt[i, t] + phiBeta[1, i, 
                t] * weight[i, t] + phiBeta[2, i, t] * weight[i, 
                t] * weight[i, t]
        }
    }
    for (i in 1:N) {
        for (t in first[i]:(last[i] - 1)) {
            phiInt[i, t] ~ dnorm(phiIntT[t], sd = 2)
            for (b in 1:2) {
                phiBeta[b, i, t] ~ dnorm(phiBetaT[b, t], sd = 1/0.667)
            }
        }
    }
    for (t in 1:(T - 1)) {
        phiIntT[t] ~ T(dnorm(0, sd = 0.667), -3.5, 3.5)
        phiIntTOut[t] <- ilogit(phiIntT[t])
        phiBetaT[1, t] ~ dnorm(0, sd = 1/0.667)
        phiBetaT[2, t] ~ dnorm(0, sd = 1/0.667)
    }
    for (i in 1:N) {
        z[i, first[i]] ~ dcat(delta[1:2])
        for (j in first[i]:(last[i] - 1)) {
            z[i, j + 1] ~ dcat(gamma[z[i, j], 1:2, j, i])
            yDATA[i, j + 1] ~ dcat(omega[z[i, j + 1], 1:2, j])
        }
    }
}

19.0.3.3 Model statistics

Run statistics
nIter nBurnin nChains thinRate
15000 10000 2 5

[1] “Run time = 6.035 hours”

19.0.3.4 Plot traces

Code
  priors <- rnorm(out_NN_OBmod3$runData$nIter * out_NN_OBmod3$runData$nChains, 0, 1000)
  MCMCtrace(object = out_NN_OBmod3$mcmc,
            #ISB = FALSE,
            #exact = TRUE, 
            params = c("phiIntTOut", "grIntT"),
            pdf = FALSE, 
            priors = priors)

19.0.3.5 Summary data

Code
  MCMCplot(object = out_NN_OBmod3$mcmc, params = c("phiIntTOut"))

Code
  MCMCplot(object = out_NN_OBmod3$mcmc, params = c("phiBetaT"))

Code
  MCMCplot(object = out_NN_OBmod3$mcmc, params = c("p"))

Code
  MCMCplot(object = out_NN_OBmod3$mcmc, params = c("grIntT"))

Summary values

Code
#|
(summaryMod3_tt_growth <- MCMCsummary(object = out_NN_OBmod3$mcmc, params = c("phiIntTOut", "p", "grIntT"), round = 3) %>%
    rownames_to_column(var = "var"))
              var   mean    sd   2.5%    50% 97.5% Rhat n.eff
1   phiIntTOut[1]  0.809 0.063  0.674  0.820 0.906 1.00    20
2   phiIntTOut[2]  0.882 0.047  0.788  0.887 0.951 4.70    30
3   phiIntTOut[3]  0.795 0.046  0.696  0.797 0.875 2.13    26
4   phiIntTOut[4]  0.748 0.031  0.680  0.749 0.802 1.40    82
5   phiIntTOut[5]  0.755 0.032  0.686  0.755 0.813 1.06    74
6   phiIntTOut[6]  0.809 0.038  0.735  0.809 0.876 1.22    33
7   phiIntTOut[7]  0.867 0.035  0.781  0.871 0.918 1.19    31
8   phiIntTOut[8]  0.470 0.092  0.293  0.463 0.635 2.15    22
9   phiIntTOut[9]  0.592 0.095  0.394  0.590 0.794 1.15    16
10 phiIntTOut[10]  0.596 0.112  0.338  0.599 0.813 1.58    16
11 phiIntTOut[11]  0.647 0.113  0.421  0.669 0.823 1.24    10
12           p[1]  0.590 0.021  0.549  0.590 0.630 1.04   177
13           p[2]  0.409 0.018  0.374  0.408 0.446 1.05   104
14           p[3]  0.704 0.019  0.665  0.704 0.741 1.01   306
15           p[4]  0.655 0.021  0.610  0.655 0.696 1.01   323
16           p[5]  0.635 0.024  0.587  0.636 0.684 1.00   311
17           p[6]  0.495 0.028  0.440  0.495 0.549 1.01   399
18           p[7]  0.746 0.033  0.682  0.745 0.808 1.06   318
19           p[8]  0.774 0.049  0.672  0.775 0.863 1.16   206
20           p[9]  0.596 0.067  0.459  0.597 0.725 1.24    79
21          p[10]  0.657 0.103  0.446  0.668 0.834 1.22    30
22          p[11]  0.812 0.133  0.531  0.832 0.993 1.05    24
23      grIntT[1]  0.122 0.027  0.064  0.122 0.171 1.01   292
24      grIntT[2]  0.128 0.024  0.081  0.128 0.178 1.17   175
25      grIntT[3]  0.899 0.029  0.842  0.899 0.956 1.35   166
26      grIntT[4]  0.327 0.024  0.282  0.325 0.377 1.12   226
27      grIntT[5]  0.075 0.033  0.011  0.075 0.140 1.03   140
28      grIntT[6]  0.170 0.033  0.101  0.171 0.234 1.04   170
29      grIntT[7]  1.642 0.044  1.550  1.643 1.724 1.02   117
30      grIntT[8]  0.722 0.053  0.616  0.719 0.826 1.02    80
31      grIntT[9] -0.164 0.091 -0.360 -0.165 0.014 1.08    28
32     grIntT[10]  0.435 0.104  0.224  0.443 0.655 1.08    25
33     grIntT[11]  2.150 0.119  1.924  2.152 2.385 1.13    41
Code
  # To get the mMCMCSummaryRMNA funcion which I adapted to deal with NAs
  source('./models/cmrNN_OB/tt_growth/modelCMR_tt_growth_NN_OB_functionsToSource.R')
  
  #summaryMod3_tt_growth_z0 <- MCMCSummaryRMNA(object = out_NN_OBmod3$mcmc, params = c("weight"), round = 3) %>%
  summaryMod3_tt_w <- MCMCSummaryRMNA(object = out_NN_OBmod3$mcmc, params = c("weight"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod3_tt_z <- MCMCSummaryRMNA(object = out_NN_OBmod3$mcmc, params = c("z"), round = 3) %>%
    rownames_to_column(var = "var") |> 
    mutate(
      ind0 = str_match(var, "([0-9]+), ([0-9]+)")[,2],
      t0 =   str_match(var, "([0-9]+), ([0-9]+)")[,3],
      ind = as.numeric(ind0),
      t = as.numeric(t0)
    ) |> 
    dplyr::select(-c(t0, ind0))
  
  summaryMod3_tt_z0 <- summaryMod3_tt_z |> 
    left_join(summaryMod3_tt_w, by = c("ind", "t"), suffix = c("_z", "_weight"))

Add other variables to summary values

Code
  ehLong <- 
    eh$eh |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      enc = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))
  
  firstLong <- eh$first |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(first = value)
  
  lastLong <- eh$last |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) |> 
    rename(last = value)
    
  cohortLong <- eh$cohort |> 
    as_tibble() |> 
    rownames_to_column("ind") |> 
    mutate(ind = as.numeric(ind)) 
  
  weightLong <- 
    eh$weight |>
    as.data.frame() |> 
    rownames_to_column("ind0") |> 
    pivot_longer(starts_with("ais")) |> 
    mutate(
      t = as.numeric(str_match(name, "([0-9]+)")[,1]),
      weightDATA = value,
      ind = as.numeric(ind0)
    ) |> 
    dplyr::select(-c(name, value, ind0))
  
  meanWeight <- mean(eh$weight, na.rm = TRUE)
  sdWeight <- sd(eh$weight, na.rm = TRUE)

  summaryMod3_tt_z <- summaryMod3_tt_z0 |> 
    left_join(ehLong) |> 
    mutate(
      meanM1 = mean_z - 1,
      pSurv = ifelse(meanM1 == 0, 1, 1 - meanM1),
      enc8 = ifelse(enc == 1, 0.8, 0)
    ) |> 
    left_join(firstLong) |> 
    left_join(lastLong) |> 
    left_join(cohortLong) |> 
    left_join(weightLong) |> 
    mutate(
      weightDATA_std = (weightDATA - meanWeight) / sdWeight, 
      resid = weightDATA_std - mean_weight
    ) 
    
  # summaryMod3_tt_z <- summaryMod3_tt_z0 |> 
  #   left_join(ehLong) |> 
  #   mutate(
  #     # meanM1 = mean - 1,
  #     # pSurv = ifelse(meanM1 == 0, 1, 1 - meanM1),
  #     enc8 = ifelse(enc == 1, 0.8, 0)
  #   ) |> 
  #   left_join(firstLong) |> 
  #   left_join(lastLong) |> 
  #   left_join(cohortLong) 
Code
  resids_mod3 <- summaryMod3_tt_z |> 
    group_by(ind) |> 
    summarize(
      meanResid = mean(abs(resid), na.rm = TRUE),
      n = n()
    ) |> 
    ungroup()

  summaryMod3_tt_zR <- 
    summaryMod3_tt_z |> 
    left_join(resids_mod3)
Code
ojs_define(numTags_OJS_mod3 = dim(eh$tags)[1])
ojs_define(summaryMod3_tt_zR_OJS = (summaryMod3_tt_zR))
#ojs_define(summary_tt_z_OJS = (summary_tt_z_mod3))

19.0.3.6 Plot predicted and observed masses for selected individuals

Select one or more individuals:

Code
viewof selectInd_mod3 = Inputs.select([...Array(numTags_OJS).keys()], {
  label: "Which fish?",
  value: 1,
  step: 1,
  multiple: true
})

summaryMod3_tt_zR_OJS_T = transpose(summaryMod3_tt_zR_OJS)
summaryMod3_tt_zR_OJS_T_selected = summaryMod3_tt_zR_OJS_T.filter((d) =>
  selectInd_mod3.includes(d.ind)
)

Black dots/line is estimated mass and orange dots are observed masses. The green line is the first observation and the red line is the last observation. The number in the upper right corner of each panel is the fish’s cohort.

Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Age/season combination" },
  y: { label: "Body mass (mg)" },
  marks: [
    Plot.frame(),
    Plot.dot(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    Plot.line(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "mean_weight"
    }),
    <!-- Plot.dot(summaryMod3_tt_zR_OJS_T_selected, { -->
    <!--   x: "t", -->
    <!--   y: "mean_gr", -->
    <!--   fill: "green" -->
    <!-- }), -->
    Plot.dot(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "weightDATA_std",
      fill: "orange"
    }),
    Plot.ruleX(summaryMod3_tt_zR_OJS_T_selected, {
      x: "first",
      y: 3,
      "stroke": "green"
    }),
    Plot.ruleX(summaryMod3_tt_zR_OJS_T_selected, {
      x: "last",
      y: 3,
      "stroke": "red"
    }),
    Plot.text(summaryMod3_tt_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 4,
        frameAnchor: "top-left",
        text: (d) => "Cohort = " + d.cohort
      })
    ),
    Plot.text(summaryMod3_tt_zR_OJS_T_selected,
      Plot.selectFirst({
        x: 1,
        y: 3.5,
        frameAnchor: "top-left",
        text: (d) => "Residual = " + d.meanResid
      })
    )
  ],
  facet: {
    data: summaryMod3_tt_zR_OJS_T_selected,
    x: "ind"
  }
})

19.0.3.7 Plot survival

Black dots/line is estimated probability of survival and orange dots are encountered yes (y = 0.8)/no (y = 0). The green line is the first observation and the red line is the last observation. The number in the upper right corner of each panel is the fish’s cohort.

Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Age/season combination" },
  y: { label: "Probability of survival" },
  marks: [
    Plot.frame(),
    Plot.dot(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "pSurv"
    }),
    Plot.dot(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "enc8",
      fill: "orange"
    }),
    Plot.line(summaryMod3_tt_zR_OJS_T_selected, {
      x: "t",
      y: "pSurv"
    }),
    Plot.ruleX(summaryMod3_tt_zR_OJS_T_selected, {
      x: "first",
      y: 1,
      "stroke": "green"
    }),
    Plot.ruleX(summaryMod3_tt_zR_OJS_T_selected, {
      x: "last",
      y: 1,
      "stroke": "red"
    })
    ,
    Plot.text(summaryMod3_tt_zR_OJS_T_selected, 
      Plot.selectFirst({
        x: 11,
        y: 1,
        text: d => d.cohort
      })
    )
  ],
  facet: {
    data: summaryMod3_tt_zR_OJS_T_selected,
    x: "ind"
  }
})

19.0.3.8 Output model summary data for Xiaowei

Code
  #write.csv(summary_tt, './models/cmrNN_OB/tt/dataOut/xiaowei/summary_tt.csv')
  
  #write.csv(summary_tt_z, './models/cmrNN_OB/tt/dataOut/xiaowei/summary_tt_z.csv')